function [L] = knn2lan(idx,wet)
%KNN2INC Returns the normalized graph Laplacian of the directed graph built by the k nearest neighbors
%   L = KNN2LAN(IDX) returns the sparse matrix L
%
%
%   Suqi Liu, 05.13.2014
[n,k] = size(idx);
ivec = repmat((1:n)',k,1);
nvec = reshape(idx,[],1);
evec = repmat((1:(n*k))',2,1);
if (nargin == 1)
    B = sparse(evec,[ivec;nvec],[ones(n*k,1);-ones(n*k,1)]);
    SB = sqrt(sum(B.*B))';
    ow = repmat(SB,k,1);
    iw = SB(nvec);
    C = sparse(evec,[ivec;nvec],[1./ow;-1./iw]);
else
    wvec = reshape(wet,[],1);
    qvec = sqrt(wvec);
    B = sparse(evec,[ivec;nvec],[qvec;-qvec]);
    SB = sqrt(sum(B.*B))';
    ow = repmat(SB,k,1);
    iw = SB(nvec);
    C = sparse(evec,[ivec;nvec],[qvec./ow;-qvec./iw]);
end
L = C'*C;
end